<template>
	<view>
		<view class="sticky-box">
			<search-input ref="searchInput"
				@searchConfirm="handleSearch($event)"></search-input>
			<view class="flex-row">
				<text class="tip-text">{{scanCode}};&nbsp;无此商品码对应信息，请在选择对应的商品</text>
			</view>
		</view>
		<view class="goods-list">
			<view class="goods-item"
				@tap="handleActiveGoods(item, index)"
				:class="{'goods-active': index === activeIndex}"
				v-for="(item, index) in goodsList" :key="index">
				<view class="flex-row goods-header">
					<view class="goods-name">
						<text>{{item.Name}}</text>
					</view>
				</view>
				<view class="flex-row goods-row">
					<view class="goods-unit">
						{{item.Manufacturer}}
					</view>
				</view>
				<view class="flex-row goods-row">
					<view class="goods-unit">
						{{item.PackageSpec}}
					</view>
				</view>
				<view class="flex-row goods-row">
					<view class="goods-unit">
						{{item.ApprovalNumber}}
					</view>
				</view>
				<view class="flex-row goods-row">
					<view class="goods-unit">
						包装单位：{{item.UnitRatio}}{{item.Unit}}/{{item.Pharmacy1Unit}}
					</view>
				</view>
			</view>
		</view>
		<view class="" style="height: 154rpx;" v-if="footerBtn"></view>
		<view class="flex-row footer-btn" v-if="footerBtn">
			<view class="common-solid-btn" @tap="cancel()">
				取消
			</view>
			<view class="common-btn" @tap="handleSaveCheck()">
				保存
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex'
	import searchInput from '@/components/search-input/search-input.vue'
	var that;
	export default {
		components: { searchInput },
		data() {
			return {
				footerBtn: true,
				searchVal: '',
				goodsList: [],
				activeIndex: -1,
			}
		},
		computed: {
			...mapState({
				userInfo: state => state.userInfo, // userInfo
				Token: state => state.Token, // Token
				scanCode: state => state.goods.scanCode, // scanCode
			})  
		},
		onLoad() {
			that = this
			const title = that.$Route.query.title ?? '';
			uni.setNavigationBarTitle({
				title: title
			})
		},
		onShow() {
			that.getServerData()
		},
		methods: {
			getServerData() {
				that.getHisGoodsList()
			},
			getHisGoodsList() {
				const Filter = ``;
				let requestData = {
					MethodCode: "App_Deal_WarehouseStockIn",
					Token: that.Token,
					Info: {
						"UserCode": that.userInfo.UserCode,
						"Kind": 'GetHisGoods_List',
						"Filter": Filter, 
					}
				};
				console.log(requestData);
				that.$request.globalRequest('', 'POST', requestData, '', 0).then(res => {
					console.log(res.data);
					let data = res.data.Data;
					if(data.length !== 0) {
						data.forEach(item => {
							item.ExpiredDateText = item.ExpiredDate ? that.$common.dateFormat('YYYY.mm.dd', new Date(item.ExpiredDate)) : '';
						})
						that.goodsList = data;
					} else {
						that.goodsList = [];
					}
				}).catch( res => {
					that.$common.showNormalToast(res);
					console.log(res);
				})
			},
			handleSearch(res) {
				that.searchVal = res.value;
				that.getHisGoodsList();
			},
			handleActiveGoods(item, index) {
				that.activeIndex = index;
			},
			cancel() {
				that.back()
			},
			back() {
				that.$Router.back()
			},
			handleSaveCheck() {
				const code = that.goodsList[that.activeIndex].Code;
				that.saveSupervisionCode(code);
			},
			saveSupervisionCode(code) {
				const Filter = `${that.scanCode}@${code}`;
				let requestData = {
					MethodCode: "App_Deal_WarehouseStockIn",
					Token: that.Token,
					Info: {
						"UserCode": that.userInfo.UserCode,
						"Kind": 'SaveGoodsCheckSupervisionCode',
						"Filter": Filter, 
					}
				};
				console.log(requestData);
				that.$request.globalRequest('', 'POST', requestData, '', 0).then(res => {
					console.log(res.data);
					let data = res.data.Data;
					if(data.length !== 0) {
						if (data[0].IsOK === '1') {
							that.$common.showNormalToast('保存成功');
							that.back();
						}
					} else {
						
					}
				}).catch( res => {
					that.$common.showNormalToast(res);
					console.log(res);
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
@import "goodsList.scss";
</style>
